package com.serverlite.framework.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtils {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static JSONArray listv2json(List<Object> list) {
        JSONArray a = new JSONArray();
        for (Object o : list) {
            if(o instanceof Map) {
                a.put(map2json((Map)o));
            } else {
                a.put(o);
            }
        }
        return a;
    }

    public static JSONObject map2json(Map<String, Object> map) {
        JSONObject o = new JSONObject();
        if(map != null){
            Iterator<String> iter = map.keySet().iterator();
            try {
                while (iter.hasNext()) {
                    String key = iter.next();
                    Object val = map.get(key);
                    if (val == null) {
                        o.put(key, JSONObject.NULL);
                    } else {
                        o.put(key, val);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return o;
    }
    
    public static Map<String,Object> json2map(JSONObject o){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            Iterator<?> iter = o.keys();
            while(iter.hasNext()){
                String key = (String) iter.next();
                Object value = o.get(key);
                map.put(key, value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    public static List<Object> json2listv(JSONArray array){
        List<Object> list = new ArrayList<Object>();
        try {
            for(int i = 0; i < array.length(); i++){
                Object o = array.get(i);
                if(o instanceof JSONObject){
                    Map<String,Object> map = json2map((JSONObject)o);
                    list.add(map);
                } else {
                    list.add(o);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
    
    public static Object[][] json2Arrays(JSONArray array){
        try {
            Object[][] arr = new Object[array.length()][array.getJSONArray(0).length()];
            for(int i = 0; i < array.length(); i++){
                JSONArray a = array.getJSONArray(i);
                for(int j = 0; j < a.length(); j++){
                    arr[i][j]=a.get(j);
                }
            }
            return arr;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static Object[] json2Array(JSONArray array){
        try {
            Object[] arr = new Object[array.length()];
            for(int i = 0; i < array.length(); i++){
                arr[i] = array.get(i);
            }
            return arr;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * List转换成JSONArray
     * @param list
     * @return
     */
    public static JSONArray list2json(List<?> list){
        try {
            return new JSONArray(net.sf.json.JSONArray.fromObject(list).toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * java对象转换成JSONObject
     * @param obj
     * @return
     */
    public static JSONObject java2json(Object obj){
        try {
            return new JSONObject(net.sf.json.JSONObject.fromObject(obj).toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * JSONObject转换成java对象
     * @param json
     * @param clazz
     * @return
     */
    public static Object json2java(JSONObject json, Class<?> clazz){
        return net.sf.json.JSONObject.toBean(
                net.sf.json.JSONObject.fromObject(json.toString()), clazz);
    }
    
    /**
     * JSONArray转换成java list
     * @param array
     * @param clazz
     * @return
     */
    @SuppressWarnings("deprecation")
    public static List<?> list2java(JSONArray array, Class<?> clazz){
        return net.sf.json.JSONArray.toList(net.sf.json.JSONArray.fromObject(array.toString()), clazz);
    }
}
